package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.maszy.bean.Staff;
import com.maszy.db.DBConnection;

public class StaffService {
	Staff service = new Staff();
	private static PreparedStatement pst;
	private static ResultSet rs = null;

	public int add(Staff obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql = "insert into Maszy_Staff(Maszy_Staff_Name,Maszy_Position_Id,Maszy_DoorStore_Id,Maszy_Staff_Remark) values(?,?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			pst.setString(1, obj.getStaff_Name());
			pst.setLong(2, obj.getPosition_Id());
			pst.setLong(3, obj.getDoorStore_Id());
			pst.setString(4, obj.getStaff_Remark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}

	public int update(Staff obj) {
		DBConnection con = new DBConnection();
		int i = 0;
		String sql = "update Maszy_Staff set Maszy_Staff_Name=?,Maszy_Position_Id=?,Maszy_DoorStore_Id=?,Maszy_Staff_Remark=? where Maszy_Staff_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst.setString(1, obj.getStaff_Name());
			pst.setLong(2, obj.getPosition_Id());
			pst.setLong(3, obj.getDoorStore_Id());
			pst.setString(4, obj.getStaff_Remark());
			pst.setLong(5, obj.getStaff_Id());
			i = pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}

	public int delete(Staff obj) {
		DBConnection con = new DBConnection();
		int i = 0;
		String sql = "delete from  Maszy_Staff  where Maszy_Staff_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);
			pst.setLong(1, obj.getStaff_Id());
			i = pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}

	public Staff queryById(Staff obj) {
		DBConnection con = new DBConnection();
		String sql = "select * from Maszy_Staff where Maszy_Staff_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);
			pst.setLong(1, obj.getStaff_Id());
			rs = pst.executeQuery();
			while (rs.next()) {
				obj = new Staff();
				obj.setStaff_Id(rs.getLong("Maszy_Staff_Id"));
				obj.setStaff_Name(rs.getString("Maszy_Staff_Name"));
				obj.setPosition_Id(rs.getLong("Maszy_Position_Id"));
				obj.setDoorStore_Id(rs.getLong("Maszy_DoorStore_Id"));
				obj.setStaff_Remark(rs.getString("Maszy_Staff_Remark"));
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}

	public List queryAll() {
		List list = new ArrayList();
		DBConnection con = new DBConnection();
		String sql = "select Maszy_Staff.*,Maszy_Position.*,Maszy_DoorStore.* from Maszy_Staff,Maszy_Position,Maszy_DoorStore where  Maszy_Staff.Maszy_Position_Id=Maszy_Position.Maszy_Position_Id and  Maszy_Staff.Maszy_DoorStore_Id=Maszy_DoorStore.Maszy_DoorStore_Id";
		try {
			pst = con.getConnection().prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				ResultSetMetaData rsmd = rs.getMetaData();
				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
					map.put(rsmd.getColumnName(i), rs.getObject(i));
				}
				list.add(map);
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
